package com.heisen.security.handler;

import com.heisen.security.annotation.ApiResourceOpen;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.reflections.Reflections;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author sen
 * @description
 * @date 2023/12/29 16:35
 */
@Slf4j
public class ApiResourceOpenHander {

    public String[] handle(){
        //扫描这个包下所有被@RestController注解修饰的类
        Reflections reflections = new Reflections("com.heisen");
        Set<Class<?>> restController = reflections.getTypesAnnotatedWith(RestController.class);

        List<String> opnUrl = new ArrayList<>();
        //遍历这些类
        restController.forEach(aClass -> {
            //获取类中所有的方法
            Method[] methods = aClass.getDeclaredMethods();
            for (int i = 0; i < methods.length; i++) {
                //遍历类里的方法，获取被我们自定义注解修饰的方法
                if(methods[i].isAnnotationPresent(ApiResourceOpen.class)){
//                    System.out.println(aClass.getSimpleName()+"类中被我自定义注解修饰的方法");
//                    System.out.println(methods[i].getName());
                    //获取类和方法中的一些信息
                    String path1 = "";
                    String path2 = "";
                    if(aClass.isAnnotationPresent(RequestMapping.class)){
                        path1=aClass.getAnnotation(RequestMapping.class).value()[0];
                    };

                    if(methods[i].isAnnotationPresent(RequestMapping.class)){
                        path2 = methods[i].getAnnotation(RequestMapping.class).value()[0];
                    };
                    if(methods[i].isAnnotationPresent(GetMapping.class)){
                        path2 = methods[i].getAnnotation(GetMapping.class).value()[0];

                    };
                    if(methods[i].isAnnotationPresent(PostMapping.class)){
                        path2 = methods[i].getAnnotation(PostMapping.class).value()[0];
                    };
                    //获取到自定义注解对象
                    ApiResourceOpen annotation = methods[i].getAnnotation(ApiResourceOpen.class);




                    if(!"".equals(path1) && !path1.endsWith("/")){
                        path1 = path1 +"/";
                    }
                    //拼接得到的访问该方法的路径
                    String url = path1+path2;
                    if(StringUtils.isNotEmpty(url) && !url.startsWith("/")){
                        url = "/"+url;
                    }

                    opnUrl.add(url);
                }
            }
        }) ;

        String [] urls={};
        return opnUrl.toArray(urls);
    }
}
